package com.tencent.stat;

import android.content.Context;
import android.content.IntentFilter;
import com.tencent.stat.common.StatCommonHelper;
import com.tencent.stat.common.StatLogger;
import com.tencent.stat.common.Util;
import com.tencent.stat.common.e;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpHost;
import org.json.JSONObject;

public class NetworkManager
{
  public static final int TYPE_NOT_WIFI = 2;
  public static final int TYPE_NO_NETWORK = 0;
  public static final int TYPE_WIFI = 1;
  private static NetworkManager g = null;
  private List a = new ArrayList(10);
  private volatile int b = 2;
  private volatile String c = "";
  private volatile HttpHost d = null;
  private e e = null;
  private int f = 0;
  private Context h = null;
  private StatLogger i = null;

  private NetworkManager(Context paramContext)
  {
    this.h = paramContext.getApplicationContext();
    this.e = new e();
    f.a(paramContext);
    this.i = StatCommonHelper.getLogger();
    e();
    b();
    a();
  }

  private boolean a(String paramString)
  {
    return Pattern.compile("(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})").matcher(paramString).matches();
  }

  private void b()
  {
    this.a.add("117.135.169.101");
    this.a.add("140.207.54.125");
    this.a.add("180.153.8.53");
    this.a.add("120.198.203.175");
    this.a.add("14.17.43.18");
    this.a.add("163.177.71.186");
    this.a.add("111.30.131.31");
    this.a.add("123.126.121.167");
    this.a.add("123.151.152.111");
    this.a.add("113.142.45.79");
    this.a.add("123.138.162.90");
    this.a.add("103.7.30.94");
  }

  private String c()
  {
    try
    {
      if (a("pingma.qq.com"))
        break label29;
      String str = InetAddress.getByName("pingma.qq.com").getHostAddress();
      return str;
    }
    catch (Exception localException)
    {
      this.i.e(localException);
    }
    label29: return "";
  }

  private void d()
  {
    label81: String str2;
    String str1 = c();
    if (StatConfig.isDebugEnable())
      this.i.i("remoteIp ip is " + str1);
    if (StatCommonHelper.isStringValid(str1))
      if (!(this.a.contains(str1)))
        break label81;
    for (Object localObject = str1; ; localObject = str2)
    {
      do
      {
        StatConfig.setStatReportUrl("http://" + ((String)localObject) + ":80/mstat/report");
        return;
        str2 = (String)this.a.get(this.f);
        localObject = str2;
      }
      while (!(StatConfig.isDebugEnable()));
      this.i.w(str1 + " not in ip list, change to:" + str2);
    }
  }

  private void e()
  {
    this.b = 0;
    this.d = null;
    this.c = null;
  }

  public static NetworkManager getInstance(Context paramContext)
  {
    if (g == null)
      monitorenter;
    try
    {
      if (g == null)
        g = new NetworkManager(paramContext);
      return g;
    }
    finally
    {
      monitorexit;
    }
  }

  void a()
  {
    if (Util.isNetworkAvailable(this.h))
    {
      if (StatConfig.g)
        d();
      this.c = StatCommonHelper.getLinkedWay(this.h);
      if (StatConfig.isDebugEnable())
        this.i.i("NETWORK name:" + this.c);
      if (StatCommonHelper.isStringValid(this.c))
        if (!("WIFI".equalsIgnoreCase(this.c)))
          break label115;
      for (this.b = 1; ; this.b = 2)
      {
        this.d = StatCommonHelper.getHttpProxy(this.h);
        if (StatServiceImpl.a())
          StatServiceImpl.e(this.h);
        label115: return;
      }
    }
    if (StatConfig.isDebugEnable())
      this.i.i("NETWORK TYPE: network is close.");
    e();
  }

  public String getCurNetwrokName()
  {
    return this.c;
  }

  public HttpHost getHttpProxy()
  {
    return this.d;
  }

  public int getNetworkType()
  {
    return this.b;
  }

  public boolean isNetworkAvailable()
  {
    return (this.b == 0);
  }

  public boolean isWifi()
  {
    return (this.b != 1);
  }

  public void onDispatchFailed()
  {
    this.f = ((this.f + 1) % this.a.size());
  }

  public void registerBroadcast()
  {
    try
    {
      this.h.getApplicationContext().registerReceiver(new a(this), new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
      return;
    }
    catch (Throwable localThrowable)
    {
      localThrowable.printStackTrace();
    }
  }

  public void updateIpList(String paramString)
  {
    if (StatConfig.isDebugEnable())
      this.i.i("updateIpList " + paramString);
    try
    {
      int j;
      int k;
      Object localObject1;
      if (!(StatCommonHelper.isStringValid(paramString)))
        break label233;
      paramString = new JSONObject(paramString);
      if (paramString.length() <= 0)
        break label233;
      Iterator localIterator = paramString.keys();
      do
      {
        do
        {
          if (!(localIterator.hasNext()))
            break label233;
          localObject1 = paramString.getString((String)localIterator.next());
        }
        while (!(StatCommonHelper.isStringValid((String)localObject1)));
        localObject1 = ((String)localObject1).split(";");
        k = localObject1.length;
        j = 0;
      }
      while (j >= k);
      Object localObject2 = localObject1[j];
      if (StatCommonHelper.isStringValid((String)localObject2))
      {
        localObject2 = ((String)localObject2).split(":");
        if (localObject2.length > 1)
        {
          localObject2 = localObject2[0];
          if ((a((String)localObject2)) && (!(this.a.contains(localObject2))))
          {
            if (StatConfig.isDebugEnable())
              this.i.i("add new ip:" + ((String)localObject2));
            this.a.add(localObject2);
          }
        }
      }
      label233: j += 1;
    }
    catch (Exception paramString)
    {
      this.i.e(paramString);
      this.f = new Random().nextInt(this.a.size());
    }
  }
}